package com.itheima.controller;
/*
    ヾ(❀ﾟ▽ﾟ)ノ 
 
 Grey1996year@163.com 
 
*/


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import org.apache.ibatis.builder.ResultMapResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

	@Autowired
	private JedisPool jedisPool;

	@Reference
	private OrderService orderService;


	@RequestMapping("/submit")
	public Result submit(@RequestBody Map map) {
		//1.从jedis中获取随机生成的验证码
		String telephone = (String) map.get("telephone");
		String validateCode_redis = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
		//2.获取用户输入的验证码
		String validateCode_keyIn = (String) map.get("validateCode");
		System.out.println(validateCode_keyIn);
		//3.比较判断用户是否输入正确
		/*if (validateCode_keyIn != null || !("".equals(validateCode_keyIn))) {
			return new Result(false, "请输入验证码");
		}*/
		if (!validateCode_redis.equals(validateCode_keyIn)) {
			return new Result(false, MessageConstant.VALIDATECODE_ERROR);
		}
		//用户输入正确
		//调用业务方法进行处理
		Result result = null;
		try {
			map.put("type", Order.ORDERTYPE_WEIXIN);
			result = orderService.order(map);
			System.out.println("................");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return result;
		}
	}

	@RequestMapping("/findById")
	public Result findById(Integer id){

		try {
			Map map = orderService.findById(id);
			return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
		}

	}



}
